<?php

//调用框架时，首先定义APP
defined("IN_CART") or die();

//定义框架的路径
defined("CPATH_FRAME")		|| define("CFRAME_PATH",dirname(__FILE__));

//加载框架公用的controller
defined("CPATH_COMMON")		|| define("CPATH_COMMON",	 CPATH_ROOT.'/common');

//定义APP的controller
defined("CPATH_CONTROLLER") || define("CPATH_CONTROLLER",CPATH_APP . '/protected/controller');

//定义APP的model，共享
defined("CPATH_MODEL")		|| define("CPATH_MODEL",	 CPATH_COMMON . '/model');

//定义APP的model，共享
defined("CPATH_VIEW")		|| define("CPATH_VIEW",		 CPATH_APP . '/protected/view');

defined("DEFAULT_C")		|| define("DEFAULT_C","index");

defined("DEFAULT_A")		|| define("DEFAULT_A","index");

abstract class CFrame {
	
	public static $_incFiles = array();
	
	public static $_objects  = array();
	
	//执行框架，调用相应的内容，
	public static function start() {
		spl_autoload_register(array('CLoad','index'));
		self::buildApp();
		CApp::run();
	}
	
	private static function buildApp() {
		//加载根目录config.inc.php,外面已经判断该文件的存在
		C(include CPATH_ROOT.'/config.inc.php');
		
		//加载公用语言包
		L(include CPATH_COMMON.'/language/common.lang.php');

		if(is_file(CPATH_COMMON.'/language/'.APP_NAME.'.lang.php')) {//加载app语言包
			L(include CPATH_COMMON.'/language/'.APP_NAME.'.lang.php');
		}
	}

	//显示404错误
	private static function display404() {
		if(is_file(VIEWDIR.'/error/404.html')) {//如果app下存在404
			self::loadFile(VIEWDIR.'/err/404.html');
		} else {//使用公用的app
			
		}
		exit();
	}

	public static function single($class) {
		if( !isset(self::$_objects[$class]) ) {
			self::$_objects[$class] = new $class();
		}
		return self::$_objects[$class];
	}
	
	public static function loadFile($file) {
		if( !isset($_incFiles[$file]) ) {
			if(!file_exists($file)) {
				Application::halt('The file:' . $fileName . ' not found!');
			}
			include $file;
			$_incFiles[$file] = true;
		}
		return $_incFiles[$file];
	}
}

//加载运行所需文件
function load_runtime_file() {
	require CPATH_FRAME . "/functions.php";
	require CPATH_FRAME . "/core/CRouter.class.php"; 
	require CPATH_FRAME . "/core/CLoad.class.php";
}

load_runtime_file();
CFrame::start();